import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, ResponseContentType , Response} from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs';
import { AppGlobal } from '../app/app.global';

@Injectable()
export class DataService {

    private http: Http;

    constructor(http: Http) {
        this.http = http;
    }

    getRequestOptions(){
        let headers = new Headers({ 
            'Content-Type': 'application/json', 
            'Accept': 'application/json',
            'Authorization' : 'Bearer ' + AppGlobal.getInstance().getUser().token
        });

        let options = new RequestOptions({ headers: headers });
        return options;
    }

    getUserDetail(){
        let url = AppGlobal.API_HOST + '/user/detail';
        return this.http.get(url, this.getRequestOptions())
            .map(res => res.json())
            .catch(this.handleError);
    }

    login(email, password){
        let url = AppGlobal.API_HOST + '/user/login'
        return this.http.post(url, {email:email, password: password})
            .map(res => res.json())
            .catch(this.handleError);
    }

    register(name, email, password){
        let url = AppGlobal.API_HOST + '/user/register'
        return this.http.post(url, {name: name, email:email, password: password})
            .map(res => res.json())
            .catch(this.handleError);
    }

    getCreatives(){
        let url = AppGlobal.API_HOST + '/creative/query'
        return this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
    }

    getGames(category_id, page_index, page_size){
        let url = AppGlobal.API_HOST + '/game/query?category_id=' + category_id + '&page_index=' + page_index + '&page_size=' + page_size
        return this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
    }

    getGameRank(game_id){
        let url = AppGlobal.API_HOST + '/game/rank?game_id=' + game_id
        return this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
    }

    getGameStatistic(id){
        let url = AppGlobal.API_HOST + '/game/statistics?id=' + id
        return this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
    }

    getGameRecords(game_id){
        let url = AppGlobal.API_HOST + '/game/records?game_id=' + game_id
        return this.http.get(url, this.getRequestOptions())
            .map(res => res.json())
            .catch(this.handleError);
    }

    getComments(game_id, limit){
        let url = AppGlobal.API_HOST + '/comment/query?game_id=' + game_id + '&limit=' + limit
        return this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
    }

    sendComment(game_id, star, content){
        let url = AppGlobal.API_HOST + '/comment/send'
        return this.http.post(url, {star : star, game_id :game_id, content: content}, this.getRequestOptions())
            .map(res => res.json())
            .catch(this.handleError);
    }

    feedback(user_id, content) {
        return this.http.post( AppGlobal.API_HOST + '/feedback', { content: content }, this.getRequestOptions())
            .map(res => res.json())
            .catch(this.handleError);
    }

    // saveGame(game_id, name0, data0, name1, data1){
    //     console.log('saveGame', game_id, name0, data0, name1, data1);
    //     let url = AppGlobal.API_HOST + '/game/save'
    //     return this.http.post(url, {game_id: game_id, name0: name0, data0: data0, name1: name1, data1: data1}, this.getRequestOptions())
    //         .map(res => res.json())
    //         .catch(this.handleError);
    // }

    getGameLib(url){
        return this.http.get(url,  {
            responseType: ResponseContentType.Blob
        })
        .map(response => (<Response>response).blob())
        .catch(this.handleError);
    }

    // getRPGLib(url){
    //     return this.http.get(url)
    //     .map(res => res.text())
    //     .catch(this.handleError);
    // }

    handleError(error) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }

}